from flask import Flask
from .config import Config
from .extensions import db, migrate, cors
from flask_cors import CORS

from app.routes.user import user_bp

from app.routes.get_history_data import history_weather_bp

from app.routes.predict_ensemble import predict_ensemble_bp
from app.routes.predict_lstm import predict_lstm_bp

from app.routes.evaluate_ensemble import evaluate_ensemble_bp
from app.routes.evaluate_lstm import evaluate_lstm_bp


from app.routes.train_ensemble import train_ensemble_bp
from app.routes.train_lstm import train_lstm_bp


def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    CORS(app, origins=["http://localhost:5173"])

    db.init_app(app)
    migrate.init_app(app, db)
    cors.init_app(app, supports_credentials=True)

    with app.app_context():
        app.register_blueprint(user_bp, url_prefix='/api')

        app.register_blueprint(history_weather_bp, url_prefix='/api')

        app.register_blueprint(predict_ensemble_bp, url_prefix='/api')
        app.register_blueprint(predict_lstm_bp, url_prefix='/api')

        app.register_blueprint(evaluate_ensemble_bp, url_prefix='/api')
        app.register_blueprint(evaluate_lstm_bp, url_prefix='/api')

        app.register_blueprint(train_ensemble_bp, url_prefix='/api')
        app.register_blueprint(train_lstm_bp, url_prefix='/api')

    return app
